package com.pri.chat.activity.vedio;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.request.RequestOptions;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.netease.nim.uikit.net.SharedHelper;
import com.netease.nim.uikit.net.entity.DTBean;
import com.pri.chat.R;
import com.pri.chat.activity.OtherPeopleMsgActivity;
import com.pri.chat.activity.vedio.widget.LikeView;
import com.pri.chat.activity.vedio.widget.SampleCoverVideo;
import com.vondear.rxtool.RxActivityTool;

import java.util.List;

import androidx.annotation.Nullable;

/**
 * 描述：添加类的描述
 *
 * @author wx5e48d604c2885118
 * @time 2020/4/8
 */
public class DetailAdapter2 extends BaseQuickAdapter<DTBean, BaseViewHolder> {

    protected RemoveItemListener removeItemListener;
    private int actual_width;
    private int actual_height;

    public DetailAdapter2(int layoutResId, @Nullable List<DTBean> data) {
        super(layoutResId, data);
    }

    @Override
    protected void convert(BaseViewHolder holder, DTBean bean) {
        if (bean == null) {
            if (null != removeItemListener)
                removeItemListener.removeItem(holder.getAdapterPosition());
            return;
        }
        SampleCoverVideo videoView = holder.getView(R.id.video_view);


        if (actual_width <= 0){
            actual_width = Utility.getsW((Activity) mContext);
        }
        if(actual_height <= 0){
            actual_height = Utility.getsH((Activity) mContext);
        }
        int image_width = bean.getWidth();
        int image_height = bean.getHeight();
        double wScale = actual_width*1.0 / image_width;
        double hScale = actual_height*1.0 / image_height;
        double scale = wScale < image_height?wScale:hScale;
        int iamgHeight = (int)(scale*image_height);
        int iamgWidth = (int)(scale*image_width);

        videoView.loadCoverImage(bean.getVideoPath()+"?vframe/jpg/offset/0",R.drawable.shape_black,iamgWidth,iamgHeight);
        videoView.setUpLazy(bean.getVideoPath(), false, null, null, "这是title");
        //增加title
        videoView.getTitleTextView().setVisibility(View.GONE);
        //设置返回键
        videoView.getBackButton().setVisibility(View.GONE);
        //设置全屏按键功能
        videoView.getFullscreenButton().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                videoView.startWindowFullscreen(mContext, false, true);
            }
        });
        //防止错位设置
        videoView.setPlayTag(TAG);
        videoView.setLockLand(true);
        videoView.setPlayPosition(holder.getAdapterPosition());
        //是否根据视频尺寸，自动选择竖屏全屏或者横屏全屏，这个标志为和 setLockLand 冲突，需要和 orientationUtils 使用
        videoView.setAutoFullWithSize(false);
        //音频焦点冲突时是否释放
        videoView.setReleaseWhenLossAudio(false);
        //全屏动画
        videoView.setShowFullAnimation(true);
        //小屏时不触摸滑动
        videoView.setIsTouchWiget(false);


        Glide.with(holder.itemView.getContext())
                .asDrawable()
                .load(bean.getHeadPic())
                .apply(RequestOptions.bitmapTransform(new CircleCrop()))
                .into((ImageView) holder.getView(R.id.author_header));

        holder.getView(R.id.author_header).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Bundle bundle = new Bundle();
                bundle.putString("id", bean.getMemberId());
                RxActivityTool.skipActivity(mContext, OtherPeopleMsgActivity.class, bundle);
            }
        });

        ((TextView) holder.getView(R.id.shareTv)).setText("分享");
        ((TextView) holder.getView(R.id.pl_num)).setText(bean.getPlNum());
        ((TextView) holder.getView(R.id.author_name)).setText(bean.getMemberName());
        ((TextView) holder.getView(R.id.item_title)).setText(bean.getContent());
        TextView gzTv = holder.getView(R.id.gzTv);
        if(SharedHelper.readId(mContext).equals(bean.getMemberId())){
            gzTv.setVisibility(View.GONE);
        }else{
            gzTv.setVisibility(View.VISIBLE);
        }
        if(bean.getSfgz() == 0){
            gzTv.setText("关注");
            gzTv.setBackgroundResource(R.drawable.btn_orange_shape);
            gzTv.setVisibility(View.VISIBLE);
        }else{
            gzTv.setText("已关注");
            gzTv.setBackgroundResource(R.drawable.btn_gray_20_shape);
            gzTv.setVisibility(View.GONE);

        }
//        ((TextView) holder.getView(R.id.comments_num)).setText(data.getConsumption().getReplyCount() + "");
        ((TextView) holder.getView(R.id.give_a_like_num)).setText(bean.getZanNum()+"");
//        holder.getView(R.id.video_view).setTag(Uri.parse(data.getPlayUrl()));
        holder.getView(R.id.video_view).setTag(bean.getVideoPath());

        if (bean.getSfdz() == 1)
            ((LikeView) holder.getView(R.id.lv_icon)).setDefaultIcon(mContext.getResources().getDrawable(R.drawable.give_like));

        holder.getView(R.id.iv_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null != mContext && mContext instanceof Activity)
                    ((Activity) mContext).finish();
            }
        });
    }

    @Override
    public void onViewRecycled(BaseViewHolder holder) {
//        ImageView img_thumb = holder.getView(R.id.img_thumb);
//        if (img_thumb != null) {
//            Glide.with(holder.itemView.getContext()).clear(img_thumb);
//        }
    }

    public void setRemoveItemListener(RemoveItemListener removeItemListener) {
        this.removeItemListener = removeItemListener;
    }
    public interface RemoveItemListener {
        void removeItem(int position);
    }

}